home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapter5 / calwndpr.c < prev    next >
C/C++ Source or Header  |  1996-01-14  |  6KB  |  195 lines

  1.  
  2. #include <windows.h>  
  3. #include "CalWndPr.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20. WNDPROC fnOldProc;
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  25.                       LPTSTR lpCmdLine, int nCmdShow)
  26. {
  27.    MSG      msg;
  28.    HWND     hWnd; 
  29.    WNDCLASS wc;
  30.  
  31.    // Register the main application window class.
  32.    //............................................
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    // Create the main application window.
  55.    //....................................
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );
  106. }
  107.  
  108.  
  109. LRESULT CALLBACK NewButtonProc( HWND hWnd, UINT uMsg, 
  110.                                 WPARAM wParam, LPARAM lParam )
  111. {
  112.    switch( uMsg )
  113.    {
  114.       case WM_KEYDOWN :
  115.               SetWindowText( hWnd, "Pressed!" );
  116.               break;
  117.  
  118.       case WM_KEYUP :
  119.               SetWindowText( hWnd, "Released!" );
  120.               break;
  121.    }
  122.  
  123.    // Call the button's original window procedure to do default
  124.    // processing.
  125.    //..........................................................
  126.    return( CallWindowProc( fnOldProc, hWnd, uMsg, wParam, lParam ) );
  127. }
  128.  
  129.  
  130. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  131. {
  132. static HWND hButton = NULL;
  133.  
  134.    switch( uMsg )
  135.    {
  136.        case WM_CREATE :
  137.                hButton = CreateWindow( "BUTTON", "Button", 
  138.                                        WS_CHILD | WS_VISIBLE,
  139.                                        10, 10, 100, 60,
  140.                                        hWnd, NULL, hInst, NULL );
  141.  
  142.                // Subclass the button.
  143.                //.....................
  144.                fnOldProc = (WNDPROC)GetWindowLong( hButton, GWL_WNDPROC );
  145.                SetWindowLong( hButton, GWL_WNDPROC, (LONG)NewButtonProc );
  146.                break;
  147.  
  148.       case WM_COMMAND :
  149.               switch( LOWORD( wParam ) )
  150.               {
  151.                  case IDM_ABOUT :
  152.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  153.                         break;
  154.  
  155.                  case IDM_EXIT :
  156.                         DestroyWindow( hWnd );
  157.                         break;
  158.               }
  159.               break;
  160.       
  161.       case WM_DESTROY :
  162.               PostQuitMessage(0);
  163.               break;
  164.  
  165.       default :
  166.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  167.    }
  168.  
  169.    return( 0L );
  170. }
  171.  
  172.  
  173. LRESULT CALLBACK About( HWND hDlg,           
  174.                         UINT message,        
  175.                         WPARAM wParam,       
  176.                         LPARAM lParam)
  177. {
  178.    switch (message) 
  179.    {
  180.        case WM_INITDIALOG: 
  181.                return (TRUE);
  182.  
  183.        case WM_COMMAND:                              
  184.                if (   LOWORD(wParam) == IDOK         
  185.                    || LOWORD(wParam) == IDCANCEL)    
  186.                {
  187.                        EndDialog(hDlg, TRUE);        
  188.                        return (TRUE);
  189.                }
  190.                break;
  191.    }
  192.  
  193.    return (FALSE); 
  194. }
  195.